package cn.singno.bob.common.utils;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.singno.bob.common.bean.HttpClientHelper;

/**
 * <p>名称：HttpClientUtils.java</p>
 * <p>描述：HttpClient工具类</p>
 * <pre>
 *    
 * </pre>
 * @author 鲍建明
 * @date 2014年9月19日 下午2:10:08
 * @version 1.0.0
 */
public class HttpClientUtils {

	
	private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
	
	
	/**
	 * GET请求，
	 * @param httpClientHelper
	 * @return  如果出现异常，返回null
	 */
    public static HttpClientResult sendGet(HttpClientHelper httpClientHelper) {
    	
		try {
			HttpClient httpclient = HttpClients.createDefault();
	    	HttpGet httpGet = httpClientHelper.getHttpGet();
			HttpResponse response = httpclient.execute(httpGet);
			return new HttpClientResult(response.getStatusLine().getStatusCode(), response.getAllHeaders(), response.getEntity());
		}  catch (Exception e) {
			e.printStackTrace();
			logger.error("httpGet send error", e);
		}
       return null;
    }
    
    /**
     * POST请求。不带文件上传   
     * @param httpClientHelper
     * @return 如果出现异常，返回null
     */
    public static HttpClientResult sendPost(HttpClientHelper httpClientHelper){
    	
    	try {
    		HttpClient httpclient = HttpClients.createDefault();
        	HttpPost httpPost = httpClientHelper.getSimpleHttpPost();
    		HttpResponse response = httpclient.execute(httpPost);
			return new HttpClientResult(response.getStatusLine().getStatusCode(), response.getAllHeaders(), response.getEntity());
		}catch (Exception e) {
			e.printStackTrace();
			logger.error("httpPost send error", e);
		}
    	return null;
    }
    
    /**
     * 带文件上传的POST请求
     * 如果没有文件时，将变成普通的POST请求
     * @param httpClientHelper
     * @return 如果出现异常，返回null
     */
    public static HttpClientResult sendPostFile(HttpClientHelper httpClientHelper){
    	HttpClient httpclient = HttpClients.createDefault();
    	HttpPost httpPost = httpClientHelper.getFileHttpPost();
		try {
			HttpResponse response = httpclient.execute(httpPost);
			return new HttpClientResult(response.getStatusLine().getStatusCode(), response.getAllHeaders(), response.getEntity());
		}catch (Exception e) {
			e.printStackTrace();
			logger.error("httpPostFile send error", e);
		}
    	return null;
    }
    
    /**
     * <p>名称：HttpClientResult.java</p>
     * <p>描述：HTTPClient请求的结果集</p>
     * <pre>
     *    
     * </pre>
     * @author 鲍建明
     * @date 2014年9月19日 下午2:10:53
     * @version 1.0.0
     */
    public static class HttpClientResult {
    	
    	private Cookie[] cookies;
    	
    	private String result;
    	
    	private Integer statusCode;
    	
    	private Header[] headers;
    	
    	private HttpEntity httpEntity;
    	
    	public HttpClientResult(){}

    	public HttpClientResult(Cookie[] cookies, Integer statusCode,
    			Header[] headers, HttpEntity httpEntity) {
    		super();
    		this.cookies = cookies;
    		this.statusCode = statusCode;
    		this.headers = headers;
    		this.httpEntity = httpEntity;
    	}
    	
    	public HttpClientResult( Integer statusCode,
    			Header[] headers, HttpEntity httpEntity) {
    		super();
    		this.statusCode = statusCode;
    		this.headers = headers;
    		this.httpEntity = httpEntity;
    	}

    	public Cookie[] getCookies() {
    		return cookies;
    	}

    	public void setCookies(Cookie[] cookies) {
    		this.cookies = cookies;
    	}

    	public String getResult() {
    		return result;
    	}

    	public void setResult(String result) {
    		this.result = result;
    	}

    	public Integer getStatusCode() {
    		return statusCode;
    	}

    	public void setStatusCode(Integer statusCode) {
    		this.statusCode = statusCode;
    	}

    	public Header[] getHeaders() {
    		return headers;
    	}

    	public void setHeaders(Header[] headers) {
    		this.headers = headers;
    	}

    	public HttpEntity getHttpEntity() {
    		return httpEntity;
    	}

    	public void setHttpEntity(HttpEntity httpEntity) {
    		this.httpEntity = httpEntity;
    	}
    }

    
    
    
}
